//index.js
import IndexModel from '../../models/IndexModel';
const indexModel = new IndexModel();
//获取应用实例
const app = getApp()

Page({
  data: {
    packageNavData: [],
    varietyNavData: [{
        id: '101',
        text: '我收藏的网站',
        shadow: 'shadow',
        last: '',
      },
      {
        id: '103',
        text: '我收藏的资讯',
        shadow: 'shadow',
        last: '',
      },
      {
        id: '105',
        text: '联系我们',
        shadow: 'shadow',
        last: 'last',
      },
    ],
    typeNavData: [{
        // id: '1001',
        // text: '产品类型',
        size: 'small',
        type: 'primary',
        src: './img/product_type.png',
      },
      {
        // id: '1002',
        // text: '上课形式',
        size: 'small',
        type: 'green',
        src: './img/course_form.png',
      },
      {
        // id: '1003',
        // text: '发展方向',
        size: 'small',
        type: 'yellow',
        src: './img/dev_orient.png',
      },
    ],
    pageShow: 'default',
    infoTop5: [],
    infoData: [],
    classifyType: 'chanpinfeilei', // 选中的border
    classifyNavType: [],
    productList: {},
  },
  onLoad: function () {
    Promise.all([
      indexModel.getInfoTop5(),
      indexModel.getIndexInfo(),
      indexModel.getIndexClassify(),
    ])
      .then(res => {
        // console.log('top 5 getIndexClassify->', res);
        let classifyNavType = res[2].JSONdata.List0;
        let classifyContent = res[2].JSONdata.List1[0];
        let classifyDesc = res[2].JSONdata.List2[0];
        classifyNavType.forEach((item, i) => {
          item = Object.assign(item, this.data.typeNavData[i]);
        });
        let productList = this.handleProductList(classifyContent, classifyDesc);
        // console.log(productList);
        this.setData({
          infoTop5: res[0].ListJson.List,
          infoData: res[1].ListJson.List,
          classifyNavType,
          productList,
        })
      }).catch(console.error)
    
    // app.userInfoReadyCallback = res => {
    //   console.log('index-js-1>', res);
    //   console.log('index-js-2>', app.globalData);

    // }
    // console.log('index')
    // wx.getUserInfo({
    //   success: res => {
    //     console.log('index-js-3>', res);
    //     // app.globalData.userInfo = res.userInfo
    //     // this.setData({
    //     //   userInfo: res.userInfo,
    //     //   hasUserInfo: true
    //     // })
    //   }
    // })
  },
  handleProductList(content, desc) {
    for (let key in content) {
      content[key].forEach(item => {
        if (desc[item.fenlei]) {
          item.value = desc[item.fenlei]
        }
      })
    }
    return content;
  },
  clickSkip(e) {
    const {
      currentTarget: {
        dataset: {
          param,
        },
      },
    } = e;
    // console.log('index->', param);
    switch (param.id) {
      case '105': // 联系我们
        wx.navigateTo({
          url: '/pages/about/about'
        })
        break;

      default:
        // 收藏
        wx.navigateTo({
          url: `/pages/collect/collect?type=${param.id}`,
        })
        break;
    }
  },
  clickTop5(e) {
    const {
      currentTarget: {
        dataset: {
          param,
        },
      },
    } = e;
    // console.log('clickTop5->', param);
    this.goToSkip(param);
  },
  showDetail(e) {
    this.goToSkip(e.detail.id);
  },
  goToSkip(id) {
    wx.navigateTo({
      url: `../detail/detail?id=${id}&tableName=jyZixun`,
    })
  },
  // click classify
  clickClassify(e) {
    const {
      currentTarget: {
        dataset: {
          param,
        },
      },
    } = e;
    if (this.data.classifyType !== param) {
      this.setData({
        classifyType: param,
      })
    }
  },
  onGotUserInfo(e) {
    // console.log(e);
    
  }
})